package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    SetmealMapper setmealMapper;
    @Override
    public SetmealDto getSetMealDto(Long id) {

        return setmealMapper.getSetMealDto(id);
    }

    /**
     * 添加套餐
     * @param setmealDto
     */
    @Override
    public void saveSetMealDto(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> pageSetMeal(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> setmealPage = new Page<>(page, pageSize);

        return setmealMapper.pageSetMeal(setmealPage,name);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    public void deleteByIds(Long[] ids) {
        //先查询套餐状态
        Integer count = this.query().eq("status", 1).in("id", ids).count();
        if (count>0) {
            throw new CustomerException("套餐正在售卖中 不能删除");
        }
        //删除套餐表数据
        this.removeByIds(Arrays.asList(ids));
        //删除套餐菜品关系表数据
        setmealDishService.update()
                .in("setmeal_id",ids)
                .remove();
    }

    @Override
    public void updateByIds(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        //先把之前关联的套餐数据删除
        setmealDishService.update().eq("setmeal_id",setmealDto.getId()).remove();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveOrUpdateBatch(setmealDishes);
    }

    /**
     * 查询套餐菜品信息
     * @param id
     * @return
     */
    @Override
    public List<DishDto> setMealDish(Long id) {

        return setmealMapper.setMealDish(id);
    }
}
